package org.xiaom.butler.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.xiaom.butler.bean.Content;
import org.xiaom.butler.bean.Department;
import org.xiaom.butler.bean.Hotel;
import org.xiaom.butler.bean.User;
import org.xiaom.butler.service.BaseService;
import org.xiaom.butler.util.Standard;
import org.xiaom.butler.util.Util;

import com.google.gson.JsonObject;

public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private BaseService userService;
	private String field = "user";

	public UserServlet() {
		super();
	}

	@Override
	public void init() throws ServletException {
		this.userService = new BaseService(User.class);
		super.init();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String url = request.getRequestURI();
		String function = url.split("\\.")[1];
		String name = url.split("/" + field + "/")[1];
		if (function.equals("do")) { // 请求是个功能性请求
			if ("add.do".equals(name)) {
				add(request, response);
				goToList(request, response, Standard.REDIRECT);
			} else if ("delete.do".equals(name)) {
				delete(request, response);
				goToList(request, response, Standard.REDIRECT);
			} else if ("edit.do".equals(name)) { 
				edit(request, response);
				goToList(request, response, Standard.REDIRECT);
			} else {
				String msg = "没找到页面";
				Util.redirectToErrorPage(request, response, msg);
			}
		} else if (function.equals("jsp")) {// 要求显示页面
			if (name.equals("add.jsp")) {
				goToAdd(request, response, null);
			} else if (name.equals("list.jsp")) {
				User bean=(User) userService.get(Util.getQueryParam(request, "id", "1"));
				goToList(request, response, Standard.FORWARD);
			} else if (name.equals("edit.jsp")) {
				goToEdit(request, response, Standard.FORWARD);
			} else {
				Util.redirectToErrorPage(request, response, "请求的路径不正确");
			}
		}
	}

	private void goToEdit(HttpServletRequest request,
			HttpServletResponse response, boolean forward) {
		User bean=(User) userService.get(Util.getQueryParam(request, "id", "1"));
		request.setAttribute("bean", bean);
		Util.forward(getServletConfig().getServletContext(), request, response,
				field+"/edit.jsp", false);

	}

	private void edit(HttpServletRequest request, HttpServletResponse response) {
		try {
			User bean=(User) userService.get(Util.getQueryParam(request, "id", "1"));
			
			String passWord=Util.getQueryParam(request, "pass_word", "");
			String mailAddress=Util.getQueryParam(request, "mail_address", "");
			
			bean.setMailAddress(mailAddress);
			bean.setPassWord(passWord);
			
			userService.saveOrUpdate(bean);
		} catch (Exception e) {
			e.printStackTrace();
			Util.redirectToErrorPage(request, response, "保存错误。请检查输入");
		}
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	private void add(HttpServletRequest request, HttpServletResponse response) {

	}

	private void delete(HttpServletRequest request, HttpServletResponse response) {

	}

	private void getOne(HttpServletRequest request, HttpServletResponse response) {

	}

	private void goToList(HttpServletRequest request,
			HttpServletResponse response, boolean mode)
			throws ServletException, IOException {
 
		String relativePath = getServletContext().getContextPath()+"/";
		if (Standard.FORWARD == mode) {
			Util.forward(getServletConfig().getServletContext(), request,
					response, relativePath, false);
		} else {
			Util.redirect(request, response, relativePath, false);
		}
	}

	private void goToAdd(HttpServletRequest request,
			HttpServletResponse response, Hotel bean) throws ServletException,
			IOException {

	}
}
